package com.cookie.module_constellation.fragment

import android.annotation.SuppressLint
import android.widget.Toast
import com.cookie.lib_base.base.BaseFragment
import com.cookie.lib_network.HttpManager
import com.cookie.lib_network.bean.ToadyData
import com.cookie.module_constellation.R
import kotlinx.android.synthetic.main.fragment_today.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 *   文件名:    TodayFragment
 *   创建者:    cookie
 *   创建时间:   2020/10/23 11:31
 *   描述:      TODO
 */
class TodayFragment(private val isToady: Boolean, val name: String) : BaseFragment(), Callback<ToadyData> {

    override fun getLayoutId(): Int {
        return R.layout.fragment_today
    }

    override fun initView() {
        if (isToady) {
            loadToday()
        } else {
            loadTomorrow()
        }
    }

    //获取今天的数据
    private fun loadToday() {
        HttpManager.queryTodayConstellInfo(name, this)
    }

    //获取明天的数据
    private fun loadTomorrow() {
        HttpManager.queryTomorrowConstellInfo(name, this)
    }

    override fun onFailure(call: Call<ToadyData>, t: Throwable) {
        Toast.makeText(activity, "加载失败", Toast.LENGTH_SHORT).show()
    }

    @SuppressLint("SetTextI18n")
    override fun onResponse(call: Call<ToadyData>, response: Response<ToadyData>) {
        val data = response.body()
        data?.let {
            tvName.text = "星座名称: ${it.name}"
            tvTime.text = "当前时间: ${it.dateTime}"
            tvNumber.text = "幸运数字: ${it.number}"
            tvFriend.text = "速配星座: ${it.QFriend}"
            tvColor.text = "幸运颜色: ${it.color}"
            tvSummary.text = "今日概述: ${it.summary}"

            pbAll.progress = it.all.substring(0,it.all.length-1).toInt()
            pbHealth.progress = it.all.substring(0,it.health.length-1).toInt()
            pbLove.progress = it.all.substring(0,it.love.length-1).toInt()
            pbMoney.progress = it.all.substring(0,it.money.length-1).toInt()
            pbWork.progress = it.all.substring(0,it.work.length-1).toInt()
        }
    }
}